package com.example.demo.controller;

import com.example.demo.dto.SearchRequest;
import com.example.demo.Entity.Item;
import com.example.demo.Service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api")
@Validated
public class SearchController {

    private final SearchService searchService;

    @Autowired
    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    /**
     * 处理搜索请求。
     *
     * @param request 包含搜索关键词的请求体。
     * @return 包含搜索结果的响应。
     */
    @PostMapping("/search")
    public ResponseEntity<List<Item>> search(@Valid @RequestBody SearchRequest request) {
        List<Item> results = searchService.search(request.getQuery());
        System.out.println(results);
        return ResponseEntity.ok(results);
    }
}